package dbinit;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import core.address.model.AddressType;
import core.address.model.BasicAddress;
import core.address.model.city.City;
import core.address.model.country.Country;
import core.util.HibernateCoreUtil;
import core.address.model.state.State;

public class InitBasicAddress {
	
	public static BasicAddress getBasicAddressByUuid(Long uuid)
	{
		BasicAddress ba = null ;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try 
		{
				transaction = session.beginTransaction();
				ba = (BasicAddress) session.get(BasicAddress.class, uuid);
				transaction.commit();
		}
		catch (HibernateException e) 
		{
			// TODO: handle exception
			transaction.rollback();
			e.printStackTrace();
		}
		finally 
		{
			session.close();
		}
		return ba ;
	}
	public static BasicAddress getBasicAddressByEmailAndAddresstype(String email , AddressType at)
	{
		BasicAddress ba = null ;
		Transaction transaction = null;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		try
		{
			transaction = session.beginTransaction();
			ba = (BasicAddress)session.createQuery("from BasicAddress ba where ba.email='"+email+"' and ba.addresstype=:addresstype").setEntity("addresstype", at).uniqueResult();
			transaction.commit();
		}
		catch(HibernateException he)
		{
			he.printStackTrace();
		}
		finally
		{
			session.close();
		}
		return ba ;
	}
	public static Long doinit(Long addressTypeId,String addressLine1 , String addressLine2 ,String phone,String mobile,String email, String fax, String homepage, String postalcode, String phonebusiness, String companyname,String firstname,String secondname,String lastname,String title ,Long countryId,Long stateId,Long cityId)
	{
		Transaction transaction = null;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		AddressType at = InitAddressType.getAddressTypeByUuid(addressTypeId);
		BasicAddress basicaddress = getBasicAddressByEmailAndAddresstype(email , at);
		if(basicaddress == null)
		{
			basicaddress = new BasicAddress();
		}
		Country country = InitCountry.getCountryByUuid(countryId);
		State s = InitState.getStateByUuid(stateId);
		City c = InitCity.getCityByUuid(cityId);
		basicaddress.setAddresstype(at);
		basicaddress.setCity(c);
		basicaddress.setCountry(country);
		basicaddress.setAddressLine1(addressLine1);
		basicaddress.setAddressLine2(addressLine2);
		basicaddress.setState(s);
		basicaddress.setPhone(phone);
		basicaddress.setMobile(mobile);
		basicaddress.setEmail(email);
		basicaddress.setFax(fax);
		basicaddress.setHomepage(homepage);
		basicaddress.setPostalcode(postalcode);
		basicaddress.setPhonebusiness(phonebusiness);
		basicaddress.setCompanyname(companyname);
		basicaddress.setFirstname(firstname);
		basicaddress.setSecondname(secondname);
		basicaddress.setLastname(lastname);
		basicaddress.setTitle(title);
		Long uuid = null;
		try
		{
			transaction = session.beginTransaction();
			basicaddress = (BasicAddress)session.merge(basicaddress);
			uuid = basicaddress.getUuid();
			transaction.commit();
		}
		catch(HibernateException he)
		{
			he.printStackTrace();
		}
		finally
		{
			session.close();
		}
		return uuid ;
	}
}
